define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {
    let profession_id = $('#profession_id').val();
    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'profession/course/index' + location.search,
                    add_url: 'profession/course/add?profession_id='+profession_id,
                    edit_url: 'profession/course/edit',
                    del_url: 'profession/course/del',
                    multi_url: 'profession/course/multi',
                    import_url: 'profession/course/import',
                    table: 'profession_course',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                queryParams: function (params) {
                    params.ids = profession_id;
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'course_id', title: __('Course_id')},
                        {field: 'course_info.name', title: __('name')},
                        {
                            field: 'exam_time',
                            title: __('Exam_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime,
                            datetimeFormat: 'YYYY-MM-DD'
                        },
                        {
                            field: 'a_time',
                            title: __('A_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'u_time',
                            title: __('U_time'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 批量设置考试时间
            $('.btn-batch-setexamtime').click(function (){
                var ids = Table.api.selectedids(table);
                if (ids.length === 0) {
                    Layer.alert(__('请选择需要操作的题库'));
                    return false;
                }
                var url = 'profession/course/batchsetexamtime?ids=' + ids.join(',');
                Fast.api.open(url, $(this).data("original-title") || $(this).attr("title") || __('批量设置考试时间'), $(this).data() || {}, {
                    success: function (data, ret) {
                        $(".btn-refresh").trigger("click");
                    }
                });
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        batchsetexamtime: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
